package com.cxs.plan.controller.power;

import com.cxs.plan.domain.HttpResult;
import com.cxs.plan.DTO.UserDTO;
import com.cxs.plan.service.UserService;
import com.cxs.plan.utils.PageUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("admin")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private PageUtil pageUtil;


    @GetMapping("/user")
    public HttpResult getUserList() {
        return HttpResult.success(pageUtil.autoPage(userService.getUserList()));
    }

    @PostMapping("/user")
    public HttpResult addUser(@RequestBody @Validated UserDTO user) {
        userService.saveUser(user);
        return HttpResult.success();
    }

    @PutMapping("/user/{id}")
    public HttpResult updateUser(@PathVariable("id") Integer id, @RequestBody @Validated UserDTO user) {
        user.setId(id);
        userService.updateUser(user);
        return HttpResult.success();
    }

    @GetMapping("/user/{id}")
    public HttpResult getUser(@PathVariable("id") Integer id) {
        return HttpResult.success(userService.getUserById(id));
    }

    @DeleteMapping("/user/{id}")
    public HttpResult deleteUser(@PathVariable("id") Integer id) {
        userService.delUserById(id);
        return HttpResult.success();
    }
}
